package com.yunji.web.base;

import com.yunji.aggregate.model.dto.base.StaffParam;
import com.yunji.aggregate.model.vo.base.StaffStatisticsVO;
import com.yunji.aggregate.service.BaseAggregateService;
import com.yunji.base.business.StaffBusiness;
import com.yunji.base.dto.StaffDTO;
import com.yunji.common.annotation.Log;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.constant.CacheCode;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.model.domain.StaffDO;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.common.utils.bean.BeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @version 1.0
 * @Project: yunji-coffee
 * @Package com.yunji.web.user
 * @FileName StaffController.java
 * @Copyright Copyright © 2024 云集互联. All Rights Reserved
 * Company		com
 * @CreateTime 2024-04-08 13:50:12
 * @Description 员工管理交互
 * @Modification Author                    History Time			Version				Description
 * --------------------------------------------------------------------------------
 * HuaAo				2024-04-07 13:50:12		1.0					1.0 Version
 * @since JDK 1.8.0_202
 */
@Api(tags = "管理端-员工管理")
@RestController()
@RequestMapping("/container/staff")
public class StaffController extends BaseController {
    @Resource
    private StaffBusiness business;

    @Resource
    BaseAggregateService aggregateService;

    @ApiOperation("查询员工列表")
    @PreAuthorize("@ss.hasPermi('container:staff:query')")
    @GetMapping("/list")
    public Response<List<StaffStatisticsVO>> list(@Valid StaffParam param) {
        List<StaffStatisticsVO> list = aggregateService.staffList(param);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("导出员工列表")
    @Log(title = "员工", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('container:staff:export')")
    @PostMapping("/export")
    public void export(StaffParam param) {
        param.setExport(true);
        List<StaffStatisticsVO> list = aggregateService.staffList(param);
        ExcelUtil<StaffStatisticsVO> util = new ExcelUtil<>(StaffStatisticsVO.class);
        util.exportExcel(response, list, "员工数据");
    }

    @ApiOperation("获取员工详细信息")
    @GetMapping(value = "/{id}")
    public Response<StaffDO> getInfo(@PathVariable("id") Long id) {
        return ResponseHelper.success(business.select(id));
    }

    @ApiOperation("新增员工")
    @Log(title = "员工", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('container:staff:add')")
    @PostMapping
    public Response<Long> create(@RequestBody StaffDTO dto) {
        return ResponseHelper.success(business.create(dto));
    }

    @ApiOperation("修改员工")
    @Log(title = "员工", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('container:staff:edit')")
    @PutMapping
    public Response<String> update(@RequestBody StaffDTO dto) {
        business.update(dto);
        return ResponseHelper.success("修改成功");
    }

    @ApiOperation("删除员工")
    @Log(title = "员工", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('container:staff:remove')")
    @DeleteMapping("/{ids}")
    public Response<String> remove(@PathVariable Long... ids) {
        business.delete(ids);
        return ResponseHelper.success("删除成功");
    }

    @ApiOperation("员工修改状态")
    @Log(title = "员工修改状态", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('container:staff:factory')")
    @PutMapping("/changeStatus")
    public Response<String> changeStatus(@RequestBody StaffDO staff) {
        business.changeStatus(staff);
        return ResponseHelper.success("修改成功");
    }

    @ApiOperation("密码修改")
    @Log(title = "员工修改默认密码", businessType = BusinessType.UPDATE)
    @PutMapping("/changePassword")
    public Response<String> changePassword(@RequestBody StaffDO staff) {
        business.changePassword(staff);
        return ResponseHelper.success("修改成功");
    }

    @ApiOperation("根据网点查找员工列表")
    @GetMapping("/listBySiteId")
    public Response<List<StaffStatisticsVO>> searchSiteStaff(StaffParam param) {
//        param.setStatus(1);
        List<StaffStatisticsVO> list = aggregateService.staffList(param);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("根据代理商找到员工列表")
    @GetMapping("/listByAgent")
    public Response<List<StaffStatisticsVO>> searchAgentStaff(StaffParam param) {
//        param.setStatus(1);
        List<StaffStatisticsVO> list = aggregateService.staffList(param);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("人工审核")
    @GetMapping("/manualCheck")
    public Response<String> manualCheck(@RequestParam("check") Integer check) {
        redisCache.set(CacheCode.APP_AUDIT, check == 1);
        return ResponseHelper.success("操作成功");
    }

    @ApiOperation("取审核结果")
    @GetMapping("/getCheckResult")
    public Response<Boolean> getCheckResult() {
        Boolean result = redisCache.get("AUDIT");
        Assert.isTrue(BeanUtils.isNotNull(result), "取出失败");
        return ResponseHelper.success(result);
    }

    /*------------------------------------搜索栏-------------------------------------*/

    @ApiOperation("员工姓名匹配搜索")
    @GetMapping("/searchName")
    public Response<List<String>> searchName(@RequestParam("name") String name) {
        return ResponseHelper.successWithPage(business.searchName(name));
    }

    @ApiOperation("员工电话匹配搜索")
    @GetMapping("/searchPhone")
    public Response<List<String>> searchPhone(@RequestParam("phone") String phone) {
        return ResponseHelper.successWithPage(business.searchPhone(phone));
    }

    @ApiOperation("代理商名称匹配搜索")
    @GetMapping("searchAgentName")
    public Response<List<String>> searchAgentName(@RequestParam("agentName") String agentName) {
        return ResponseHelper.successWithPage(business.searchAgentName(agentName));
    }

    @ApiOperation("网点名称匹配搜索")
    @GetMapping("searchSiteName")
    public Response<List<String>> searchSiteName(@RequestParam("siteName") String siteName) {
        return ResponseHelper.successWithPage(business.searchSiteName(siteName));
    }

}
